<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>libdill</title>
  <link rel="stylesheet" type="text/css" href="main.css">
</head>
<body>

<h1>libdill: Structured Concurrency for C</h1>

<ul id='toplist'>
<li><a href="index.html">Home</a></li>
<li><a href="download.html">Download</a></li>
<li><a href="documentation.html">Documentation</a></li>
<li><a href="tutorial.html">Tutorials</a></li>
<li><a href="faq.html">FAQ</a></li>
</ul>
<h1 id="name">NAME</h1>
<p>prefix_attach_mem - creates PREFIX protocol on top of underlying socket</p>
<h1 id="synopsis">SYNOPSIS</h1>
<div class="sourceCode"><pre class="sourceCode c"><code class="sourceCode c"><span class="pp">#include </span><span class="im">&lt;libdill.h&gt;</span>

<span class="dt">int</span> prefix_attach_mem(
    <span class="dt">int</span> s,
    <span class="dt">size_t</span> hdrlen,
    <span class="dt">int</span> flags,
    <span class="kw">struct</span> prefix_storage* mem);</code></pre></div>
<h1 id="description">DESCRIPTION</h1>
<p>PREFIX is a message-based protocol to send binary messages prefixed by size. The protocol has no initial handshake. Terminal handshake is accomplished by each peer sending size field filled by 0xff bytes.</p>
<p>This function instantiates PREFIX protocol on top of the underlying protocol.</p>
<p>This function allows to avoid one dynamic memory allocation by storing the object in user-supplied memory. Unless you are hyper-optimizing use <strong>prefix_attach</strong> instead.</p>
<p><strong>s</strong>: Handle of the underlying socket. It must be a bytestream protocol.</p>
<p><strong>hdrlen</strong>: Size of the length field, in bytes.</p>
<p><strong>flags</strong>: If set to <strong>PREFIX_BIG_ENDIAN</strong> (also known as network byte order, the default option) the most significant byte of the size will be sent first on the wire. If set to <strong>PREFIX_LITTLE_ENDIAN</strong> the least signiticant byte will come first.</p>
<p><strong>mem</strong>: The structure to store the newly created object in. It must not be deallocated before the object is closed.</p>
<p>The socket can be cleanly shut down using <strong>prefix_detach</strong> function.</p>
<p>This function is not available if libdill is compiled with <strong>--disable-sockets</strong> option.</p>
<h1 id="return-value">RETURN VALUE</h1>
<p>In case of success the function returns newly created socket handle. In case of error it returns -1 and sets <strong>errno</strong> to one of the values below.</p>
<h1 id="errors">ERRORS</h1>
<ul>
<li><strong>EBADF</strong>: Invalid handle.</li>
<li><strong>EINVAL</strong>: Invalid argument.</li>
<li><strong>EMFILE</strong>: The maximum number of file descriptors in the process are already open.</li>
<li><strong>ENFILE</strong>: The maximum number of file descriptors in the system are already open.</li>
<li><strong>ENOMEM</strong>: Not enough memory.</li>
<li><strong>ENOTSUP</strong>: The handle does not support this operation.</li>
<li><strong>EPROTO</strong>: Underlying socket is not a bytestream socket.</li>
</ul>
<h1 id="example">EXAMPLE</h1>
<div class="sourceCode"><pre class="sourceCode c"><code class="sourceCode c"><span class="dt">int</span> s = tcp_connect(&amp;addr, <span class="dv">-1</span>);
s = prefix_attach(s, <span class="dv">2</span>, <span class="dv">0</span>);
msend(s, <span class="st">&quot;ABC&quot;</span>, <span class="dv">3</span>, <span class="dv">-1</span>);
<span class="dt">char</span> buf[<span class="dv">256</span>];
<span class="dt">ssize_t</span> sz = mrecv(s, buf, <span class="kw">sizeof</span>(buf), <span class="dv">-1</span>);
s = prefix_detach(s, <span class="dv">-1</span>);
tcp_close(s);</code></pre></div>
<h1 id="see-also">SEE ALSO</h1>
<p><strong>hclose</strong>(3) <strong>mrecv</strong>(3) <strong>mrecvl</strong>(3) <strong>msend</strong>(3) <strong>msendl</strong>(3) <strong>prefix_attach</strong>(3) <strong>prefix_detach</strong>(3)</p>
</body>
